#coding:utf8
from config import *
from routin import *
from gui import *
from gui2 import *
from mpp import *

def on_move(x,y):
    cfg.last_active_time = time.time()
    if (cfg.enter_pressed or cfg.x2_pressed) and not cfg.is_suo:
        cfg.enter_pressed_then_other = True
        cfg.x2_pressed_then_other = True
        cfg.is_suo = True
        cfg.suo_dis = max(1, cal_dis(get_cur_pos() , cfg.fix_pos))
        cfg.suo_to = cfg.to

    if (cfg.enter_pressed or cfg.x2_pressed ) and cfg.is_suo:
        cfg.enter_pressed_then_other = True
        cfg.x2_pressed_then_other = True
        if cfg.x2_pressed and not cfg.w2showing:
            clear(True)
            cfg.show_one_on = True
            cfg.force_show = True

        d =  cal_dis(get_cur_pos(),cfg.fix_pos)- cfg.suo_dis
        to_lock.acquire()
        if d >= 0:
            cfg.to = cut_to(cfg.suo_to + d * get_suo_di())            
        else:
            cfg.to = cfg.suo_to * cal_dis(get_cur_pos(),cfg.fix_pos) / cfg.suo_dis
        
        set_to_chg()       
        cfg.scroll_one = 0 
        to_lock.release()
        
    cfg.last_move_time = time.time()
    
    if float(cfg.sum) != 0  and time.time()-cfg.key_input_time>0.1 and not\
          (cfg.x2_mode and cfg.x2_pressed):
        x, y = getp(cfg.last_mouse_pos, False)
        sx, sy = cfg.fix_pos
        dx = x - sx
        dy = y - sy
        cfg.last_m = (dx,dy)
        dx = round(dx*float(cfg.sum) + dx)
        dy = round(dy*float(cfg.sum) + dy)
        
        cfg.move_task = (sx + dx - cfg.last_mouse_pos[0], sy + dy - cfg.last_mouse_pos[1], 1)
        cfg.move_task_time = time.time()
        print('move_task', cfg.move_task)
        cfg.isShow = False
        clear(False)
        
    if cfg.x2_mode and not cfg.x2_pressed:
        cfg.x2_mode = False
        x, y = get_cur_pos()
        cfg.move_task = (cfg.x2_mode_dst[0] - x,cfg.x2_mode_dst[1] - y, 2)
        cfg.move_task_time = time.time()        
        print('move_task', cfg.move_task)
        cfg.isShow = False
        clear(False)                        
   
def on_click(x, y, button, pressed):
    on_click2(x, y, button, pressed,False)

def deal_chu():
    if cfg.sum != '0':
        cfg.chu_pressed_then_other = True
        if len(cfg.sum)>1 and not cfg.lock:
            cfg.sum = cfg.sum[0:-1]
        else:
            set_cfg_sum_0()    
        return
    if not cfg.chu_pressed_then_other:
        update_du(True)
    
def on_press_inner1(key,a):       
    if a >= 0 and a <= 10:
        if a == 10 and cfg.sum[-1] == '.':
            cfg.sum = cfg.sum[:-1]
            if len(cfg.sum) == 1:
                a = int(cfg.sum)
                if a >= 1 and a <= 9:
                    deal_enter_pressed('', a)
                    return                
            dotdot()
            return
            
        if a == 0 and cfg.sum == '0':
            if is_sca():
                if get_cur_pos() == cfg.sca_cur_pos:
                    mouse.Controller().position = getp()
                    clear_sca()
                    return 
                update_du(False)
                return 
            clear(True)
            cfg.show_one_on = True
            return 
        
        cfg.sum = deal_sum(cfg.sum, a, sum_max_l)
        print('update sum', cfg.sum)
        
    if a == 14:
        deal_chu()
            
    if a == 12: # -
        if is_ff(cfg.sum):
            deal_e(True,1)
            return
        if cfg.min_ge!=0 and (not cfg.w2showing or len(cfg.w2snap)<=1) and not cfg.min_ge_force_not_show:
            cfg.min_ge_force_not_show = True
            cfg.min_ge_force_show = False
            return
        
        if cfg.sum=='0' and cfg.w2showing and not cfg.x2_mode:
            if cfg.scroll_one==0:
                cfg.scroll_one = 1
            elif cfg.scroll_one==1:
                cfg.scroll_one = 2
            else:
                cfg.scroll_one = 1
            
            return
        
        if cfg.sum != '0':
            deal_e(True)
            cfg.click_time = time.time()
            return
        mouse.Controller().click(mouse.Button.left)            
            
    if a == 13: #*
        if cfg.sum!='0':
            xing()       
            return        
        if cfg.w2showing and cfg.min_ge==0:
            cfg.sum = str(xing_ge)
            xing()
            return
        ori = cfg.min_ge
        if not cfg.w2showing:
            clear(True)
            cfg.show_one_on = True
            cfg.force_show = True                
        ge_add_xing(ori)
        
def deal_ctrl_move(st):
    if len(cfg.sum)<2:
        return
    
    si = cfg.sum[0]
    v =  cfg.sum[1:]
    if 'ctrl_l' not in st:
        si = cfg.sum[-1]
        v = cfg.sum[:-1]
    
    if si not in '12346789':
        return    
    dx,dy = arrm[int(si)]
    d = int(float(v))       
    set_cfg_sum_0()     
    do_step_move(dx*d,dy*d)        

def set_key_sign(key, a):    
    st = str(key)
    if 'ctrl' in st and not   cfg.ctrl_pressed:                
        cfg.ctrl_pressed = True
        cfg.ctrl_pressed_then_other = False        
        cfg.ctrl_pressed_any_other_key = False
        if cfg.alt_pressed:
            cfg.ctrl_pressed_any_other_key = True
        if cfg.lock:
            cfg.ctrl_ori_lock = True
            cfg.lock_xing_mode = True
            cfg.lock_jia_mode = False
        else:
            clear_lock()
            cfg.lock = True
            cfg.ctrl_ori_lock = False

    if 'alt' in st and not cfg.alt_pressed:
        cfg.ctrl_ori_lock = False
        cfg.alt_pressed = True
        cfg.alt_pressed_then_other = False
        if cfg.ctrl_pressed:
            cfg.ori_xing_mode = cfg.lock_xing_mode
            cfg.lock_xing_mode = True
            cfg.lock_jia_mode = False           

    if pause_str in st:
        if not cfg.pause_pressed:
            cfg.pause_mode = True
            cfg.pause_pressed = True
            cfg.pause_pressed_then_other = False            
        else:
            cfg.pause_pressed_then_other = True

    if a == 15:
        if not cfg.enter_pressed:
            cfg.enter_pressed = True
            cfg.enter_pressed_then_other = False
        else:
            cfg.enter_pressed_then_other = True

    if a==11:
        if not cfg.add_pressed:
            cfg.add_pressed = True
            cfg.add_pressed_then_other = False
        else:
            cfg.add_pressed_then_other = True           

def pause_m():
    cfg.show_one_on =True
    x, y = get_cur_pos()
    idx = get_screen_indx(x, y)
    aa,b,c,d =  screen_config_for_indx[idx]    
    a = min(9,float(cfg.sum))
    g = 0
    if a == 9:
        g = 5 

    if cfg.pause_mode:
        dx = round(( c-aa) / 9 * a+aa)
        move_and_set_fix(dx - g, y)
        
    else:
        dy = round((d-b)/9*a+b)
        move_and_set_fix(x ,dy- g)        

def deal_pause_pressed(st, a):    
    if 'caps_lock' in st:
        cfg.pause_move = get_cur_pos()
        return
    
    if a == 15:        
        cfg.move_to_win = True
        return
    
    if a==12:
        set_cfg_sum_0()
        cfg.pause_mode = True
        return
    
    if a==14:
        if len(cfg.sum)>=2:
            cfg.sum = cfg.sum[0:-1]
        return
    
    if a==11:
        set_cfg_sum_0()
        cfg.pause_mode = False
        return
        
    if a == 13:
        x, y = get_cur_pos()
        idx = get_screen_indx(x, y)
        idx += 1
        if idx >= len(screen_config_for_indx):
            idx = 0
        aa,b,c,d =  screen_config_for_indx[idx]        
        move_and_set_fix((aa+c) //2, (b+d) //2)          
        return
    
    if (a >= 0 and a <= 10):
        if len(cfg.sum)==1 and a!=10:
            cfg.sum = str(a)
        else:
            cfg.sum = deal_sum(cfg.sum, a, sum_max_l)
        print('p',cfg.sum)
        pause_m()        

def deal_cmd(st, a):    
    if a < 0:
        return False
    if a == 11:
        cfg.always_show_one = not cfg.always_show_one
        return
       
    if a == 13:
        cfg.show_mouse_tip = not cfg.show_mouse_tip
        return
    
    if a == 12:
        cfg.show_mouse_tip2 = not cfg.show_mouse_tip2
        return
    
    if a == 14:    
        cfg.left_mouse = []
        cfg.right_mouse = []
        print('clear mouse')
        return False
    
    if a == 16:
        cfg.set_mode = True
        return False
    
    if a >= 0 and a <= 10:
        move_p(a)
        return False
    
    return False

def deal_enter_pressed(st, a,show=False):    
    if a == 0:
        cfg.isShow = False
        clear(True)
        return
    
    if a==13:     
        print('hit')   
        cfg.to_exit = True        

    if a == 11:
        cfg.always_show_one = not cfg.always_show_one
        return
       
    if a == 10:
        cfg.show_mouse_tip = not cfg.show_mouse_tip
        return
    
    if a == 12:
        cfg.show_mouse_tip2 = not cfg.show_mouse_tip2
        return
    
    if a == 14:    
        cfg.left_mouse = []
        cfg.right_mouse = []
        print('clear mouse')
        return False
    if 'caps' in st:
        cfg.set_mode  =True
        return False
    
    use_pos = False
    if cfg.sum != '0':
        deal_e(False)
        use_pos = True        
        
    if a == 5:
        cfg.cmd_mode = True
        return
    
    if a not in arrm:
        return
    clear_lock()
    dx, dy = arrm[a]    
    eg = enter_gap
    if show:
        eg = enter_gap2
    dx = -dx * eg
    dy = -dy * eg
    print('enter', st, a)
    clear(False, False)
    if use_pos:
        p = cfg.fix_pos
    else:
        p = get_cur_pos()
    cfg.fix_pos = (p[0] + dx, p[1] + dy)
    if show:
        cfg.isShow = True
    else:
        cfg.to = enter_ge
        to_lock.acquire()
        set_to_chg()
        to_lock.release()
        cfg.isShow =False
        cfg.show_one_on = True
        cfg.show_one_onetime = True        

def xing_mode_enter():
    cfg.enter_pressed_then_other = True
    if '0.' == cfg.sum[0:2]:
        a = -int( cfg.sum.replace('0.', ''))               
    else:
        a = float(cfg.sum)
    set_cfg_sum_0()    
    dx,dy = arrm[cfg.lock_xing_last_move]
    do_step_move( round(a*dx*lock_get()), round(a*dy*lock_get()))

def deal_lock_outer(st,a):    
    if a==15:
        cfg.enter_pressed_then_other = True
        if  (cfg.lock_xing_mode and cfg.sum!='0'):
            xing_mode_enter()
            return True
        cfg.lock_xing_mode = False
        cfg.lock_jia_mode =False
        return True

    if a == 11:
        cfg.lock_jia_mode = not cfg.lock_jia_mode
        cfg.lock_xing_mode = False
        return True
    
    if a == 13:
        cfg.lock_xing_mode = not cfg.lock_xing_mode
        cfg.lock_jia_mode = False
        return True
    
    if a == 14:
        deal_chu()
        return True
    
    if not cfg.lock_xing_mode and not cfg.lock_jia_mode:
        if  a >= 0 and a <= 10:
            deal_lock(a)
            set_cfg_sum_0()
            return True
        return False
    
    if cfg.lock_xing_mode:        
        if a >= 0 and a <= 10:
            cfg.sum = deal_sum(cfg.sum, a, sum_max_l)
            return True
        
        if a == 15:
            xing_mode_enter()
            return True
        
        return False
    
    if cfg.lock_jia_mode:
        if a in (0, 10):
            cfg.sum = deal_sum(cfg.sum, a, sum_max_l)
            return True
        
        if a >= 1 and a <= 9:
            if cfg.sum == '0':                
                dx,dy = arrm[cfg.lock_xing_last_move]
                do_step_move(a*dx*lock_get(), a*dy*lock_get())
                
            elif '0.' in cfg.sum:
                set_cfg_sum_0()                
                dx,dy = arrm[cfg.lock_xing_last_move]
                do_step_move(-a*dx*lock_get(), -a*dy*lock_get())
                
            set_cfg_sum_0()            
            return True
        
        return False
    return False                

def deal_x2_mode(a):
    if a>=0 and a<=10:
        cfg.sum = deal_sum(cfg.sum, a, sum_max_l)
        deal_x2_mode_num()
        return
    if a==16:
        set_cfg_sum_0()
        return
    if a==14:
        if len(cfg.sum)==1:
            set_cfg_sum_0()
        else:
            cfg.sum = cfg.sum[0:-1]
        return
    if a==15:
        cfg.x2_mode = False
        if cfg.sum=='0':
            mouse.Controller().position = cfg.x2_mode_start_pos
            return
        set_cfg_sum_0()
        mouse.Controller().position = (cfg.x2_mode_dst[0],cfg.x2_mode_dst[1])
        clear(True)  
        return       
    if a==12:
        cfg.x2_mode = False
        set_cfg_sum_0()
        mouse.Controller().position = (cfg.x2_mode_dst[0],cfg.x2_mode_dst[1])
        clear(True)          
        cfg.click_time = time.time()    

    if a==11:
        en = int(cfg.snap[-1][0])
        st = int(cfg.snap[0][0])
        if cfg.sum=='0':
            cfg.sum = str(st)
            deal_x2_mode_num()
            return
         
        if float(cfg.sum)>=en:
            cfg.sum = str(st)        
            deal_x2_mode_num()
            return
        cfg.sum = str(int(float(cfg.sum)+1))
        deal_x2_mode_num()
        return
    
    if a==13:
        en = cfg.snap[-1][0]
        st = int(cfg.snap[0][0])
        if cfg.sum=='0':
            cfg.sum = str(en)        
        if float(cfg.sum)<=st:
            cfg.sum = str(en)        
            deal_x2_mode_num()
            return    
        cfg.sum = str(math.ceil(float(cfg.sum)-1))
        deal_x2_mode_num()
       
def on_press_inner2(key,a):    
    st = str(key)        
    if cfg.ctrl_pressed and cfg.alt_pressed and a==13:
        a = 10
    if a==14 and not cfg.chu_pressed:
        cfg.chu_pressed = True
        cfg.chu_pressed_then_other = False

    if a == -2:
        return
                  
    if a==-1 and pause_str not in st and 'ctrl' not in st and 'alt' not in st:
        clear_lock()

    if cfg.x2_mode:
        if a >=0:        
            cfg.add_pressed_then_other = True
            cfg.enter_pressed_then_other = True
            deal_x2_mode(a)
            return
        elif pause_str in st:
            cfg.x2_mode = False
            cfg.pause_pressed_then_other = True    
            set_cfg_sum_0()     
            mouse.Controller().position = (cfg.x2_mode_dst[0],cfg.x2_mode_dst[1])
            clear(True)  
            return
        else:
            cfg.x2_mode = False    
            clear(True)    
    
    set_key_sign(key, a)
    
    if cfg.cmd_mode:
        cfg.add_pressed_then_other = True
        cfg.enter_pressed_then_other = True
        cfg.cmd_mode = deal_cmd(st, a)
        print('cmd', cfg.cmd_mode)
        return
    
    if cfg.set_mode:
        cfg.add_pressed_then_other = True
        cfg.set_mode = deal_set(st, a)
        cfg.enter_pressed_then_other = True
        print('set', cfg.set_mode)
        return    
    
    if cfg.pause_pressed and pause_str not in st:
        deal_pause_pressed(st, a)
        cfg.add_pressed_then_other = True
        cfg.pause_pressed_then_other = True
        cfg.enter_pressed_then_other = True
        return
    
    if cfg.enter_pressed and a != 15:
        cfg.add_pressed_then_other = True
        cfg.enter_pressed_then_other = True
        deal_enter_pressed(st, a)
        return
    
    if cfg.add_pressed and a>=1 and a<=9:
        cfg.add_pressed_then_other = True
        deal_enter_pressed(st, a,True)
        return
    
    if scroll_str in st:
        a = abs(get_num())
        ss = cfg.sum        
        if cfg.sum!='0':
            move_p(a)
            if '0.' not in ss and ss[-1]!='.':            
                cfg.click_time = time.time()
            return
        mouse.Controller().click(mouse.Button.left, 2)
        print('click2')
        
    if 'caps_lock' in st:
        deal_pressed()
                               
    if cfg.lock :        
        if (a>=0 and a<=10) or a in (11,13,14,15):
            if cfg.alt_pressed:
                cfg.alt_pressed_then_other = True
            cfg.ctrl_pressed_then_other = True
            cfg.enter_pressed_then_other = True
            cfg.add_pressed_then_other = True
            r = deal_lock_outer(st,a)
            return        
        
    if a == 16:
        if not cfg.lock and  cfg.sum != '0':
            re = cfg.sum
            set_cfg_sum_0()                                    
            return
        if not cfg.lock:
            clear_lock()
            cfg.lock = True
            return
        cfg.lock =False
        return
    
    if a>=0 and a<=15:
        on_press_inner1(key,a)
        return
    
def chg_alt_mode():
    if cfg.alt_mode==1:
            cfg.alt_mode = 2
            return
    cfg.alt_mode=1

def alt_fire():
    if  cfg.sum=='0':        
        return    
    
    x, y = get_cur_pos()
    idx = get_screen_indx(x, y)
    aa,b,c,d =  screen_config_for_indx[idx]        
    dx = x-aa
    dy = y-b

    idx = get_screen_indx(x, y)
    if cfg.sum[-1]=='.' or cfg.sum=='0.0':
        idx += 1
        if idx >= len(screen_config_for_indx):
            idx = 0    
    aa,b,c,d =  screen_config_for_indx[idx]
    f = int(float(cfg.sum))
    if cfg.sum=='0.0':
        x = dx+aa
        if not alt_fix_y:
            y = dy+b

    elif cfg.alt_mode==1:
        x = f+aa
        if not alt_fix_y:
            y = dy+b
    else:
        y = f+b
        x = dx+aa

    set_cfg_sum_0()
    mouse.Controller().position = (x,y)
    clear(True)
    cfg.set_fix_time = time.time()

def deal_alt_mode(a,st):
    if a>=0 and a<=10:
        if a==10:
            a='.'
        if len(cfg.sum)>sum_max_l:
            return
        pp = ''
        if a=='.':
            pp ='0'
        if cfg.sum=='0' :
            cfg.sum = ''+pp
        if cfg.sum=='-0':
            cfg.sum = '-'+pp
        cfg.sum += str(a)
        return
    
    if a==12:
        if cfg.sum[0]=='-':
            cfg.sum = cfg.sum[1:]
        else:
            cfg.sum = '-'+cfg.sum
        return
     
    if a==14:
        set_cfg_sum_0()
        return
    
    if a==11:
        chg_alt_mode()
        return
    if a==15:
        alt_fire()

def on_press_inner3(key,st,a):        
    cfg.x2_pressed_then_other = True
    if 'ctrl' not in st and 'alt' not in st:
        cfg.ctrl_pressed_any_other_key = True

    cfg.last_active_time = time.time()
    cfg.key_input_time = time.time()
    if '<3>' in st and cfg.ctrl_pressed:
        cfg.pause_pressed_then_other = True
        cfg.cmd_mode = True
        return
    
    if 'alt' in st and not cfg.ctrl_pressed and cfg.alt_mode==0:
        cfg.alt_pressed = True
        print('set alt mode')
        if 'alt_l' in st:
            cfg.alt_mode = 1
        else:
            cfg.alt_mode = 2
        if cfg.sum[0:2]=='0.' and cfg.sum[-1]!='.' and cfg.sum!='0.0':
            cfg.sum = cfg.sum.replace('0.','-')
            if cfg.sum=='-':
                cfg.sum = '-0'                            
        if cfg.sum[-2:]=='.0' and cfg.sum!='0.0':
            cfg.sum = '-'+cfg.sum.replace('0.','')

    if cfg.alt_mode!=0:
        deal_alt_mode(a,st)
        return
    
    on_press_inner2(key,a)
    cfg.last_mouse_pos = get_cur_pos()

def set_pressed(st,a,v):
    if 'ctrl' in st:
        cfg.ctrl_pressed = v
    if 'alt' in st:
        cfg.alt_pressed = v        
    if a==14:
        cfg.chu_pressed = v
    if a==11:
        cfg.add_pressed = v
    if a ==15 :
        cfg.enter_pressed = v
    if pause_str in st :
        cfg.pause_pressed = v

def on_press(key):
    st = str(key)
    a = decode(key, True)    
    on_press_inner3(key,st,a)
    set_pressed(st,a,True)

def valid_pause_num():
    if cfg.sum=='0':
        return False
    
    if is_ff(cfg.sum):
        return True
    
    if float(cfg.sum)<100:
        return True
    return False

def pause_change_sum():
    a,b = change_sum_10(cfg.sum)
    cfg.pause_mode = a
    cfg.sum = str(b)     

def on_release(key):
    cfg.key_input_time = time.time()
    st = str(key)
    a = decode(key, False)
    set_pressed(st,a,False)    

    if 'alt' in st:
        if cfg.alt_mode!=0:     
            print('alt fire')   
            alt_fire()
            cfg.alt_mode = 0    
            return
        
    if cfg.alt_mode!=0:        
        return
    
    if cfg.ctrl_pressed and cfg.alt_pressed and a==13:
        a = 10

    if a==11:        
        if cfg.add_pressed_then_other:
            return
        if cfg.lock:
            return
        deal_add()

    if a ==15 :        
        cfg.is_suo = False
        if cfg.enter_pressed_then_other:
            return        
        if cfg.lock:            
            return        
        deal_e(True,2)
        
    if pause_str in st :
        if cfg.pause_pressed_then_other:
            set_cfg_sum_0()
            return        
        
        if cfg.lock:            
            cfg.cmd_mode = True
            return    
        
        if not cfg.w2showing and cfg.sum=='0' and cfg.min_ge!=0:
            ge_add_ctrl(cfg.min_ge)
            return
            
        if str(cfg.sum)=='0' and cfg.w2showing:
            ctrl2()
            return
        
        if not cfg.w2showing and valid_pause_num():
                pause_change_sum()
                pause_m()
                set_cfg_sum_0()
                return
        
        if cfg.sum!='0':            
            if  not cfg.w2showing:
                clear(True)
                cfg.force_show = True
                cfg.show_one_on = True                
                return
            to_lock.acquire()
            d = get_num()            
            cfg.to = cut_to(cfg.to+d)
            set_cfg_sum_0()
            set_to_chg()
            to_lock.release()
            return
                
        if  time.time() - cfg.pause_press_time  < 0.3:
            cfg.pause_key = not cfg.pause_key
            cfg.pause_press_time = 0
            return
        
        cfg.pause_press_time = time.time()            
        
    if 'ctrl_l' in st:
        if time.time() - cfg.ctrl_l_time < 0.2:   
            cfg.ctrl_l_time = 0         
            if not cfg.w2showing and cfg.sum=='0' and cfg.min_ge!=0:
                ge_add_ctrl(cfg.min_ge)
            else:
                ctrl2()
        else:
            cfg.ctrl_l_time = time.time()
        
    if 'ctrl' in st :
        if not cfg.ctrl_pressed_any_other_key:
            cfg.lock = False
            deal_ctrl_move(st)
            print('ctrl ret')
            return
        
        cfg.ctrl_pressed = False
        cfg.is_suo = False
        if cfg.ctrl_ori_lock and cfg.ctrl_pressed_then_other:
            if cfg.lock_xing_mode and cfg.sum!='0':
                xing_mode_enter()
            cfg.lock_xing_mode = False
        else:
            cfg.lock = False

    if 'alt' in st :        
        cfg.alt_pressed = False
        if cfg.ctrl_pressed:
            if not cfg.ctrl_pressed_any_other_key:
                print('alt ret')
                return
            
            if cfg.lock and cfg.lock_xing_mode and cfg.sum!='0':
                xing_mode_enter()
                print('xing')
            cfg.lock_xing_mode = False
            cfg.lock_jia_mode = False
                
if __name__ == '__main__':    
    def win32_event_filter(msg, data):
        if cfg.enter_pressed or cfg.x2_pressed:
            return
        if (str(msg)=='522' and  cfg.w2showing and not cfg.isShow ):
            print(msg,data)
            lis.suppress_event()
            
    lis = mouse.Listener(win32_event_filter=win32_event_filter)
    lis.start()

    jumpT = threading.Thread(target=jump_bg_thr)
    jumpT.daemon = True
    jumpT.start()
    thrb = threading.Thread(target=read_mouse_q_thr)
    thrb.daemon = True
    thrb.start()
    
    mouse_listener = mouse.Listener(on_move=on_move,on_click=on_click)
    mouse_listener.start()        
    moveT = threading.Thread(target=moveF)
    moveT.daemon = True
    moveT.start()
    load_pos()
    listener = keyboard.Listener(
        on_release=on_release,
        on_press=on_press,
    )
    
    listener.start()        
    app = QApplication([])   
    w2 = my77()
    w3 = my3()
    w4 = my4()
    q = mp.Queue()    
    p = mp.Process(target=pro_input, args=(q,))
    p.daemon = True
    p.start()
    print('pid', os.getpid(), p.pid)        

    def chk_p_thr():
        p.join()
        cfg.to_exit = True
    chpT = threading.Thread(target=chk_p_thr)
    chpT.daemon = True
    chpT.start()    
                
    tt = threading.Thread(target=pro_get,args=(q,))
    tt.daemon = True
    tt.start()
        
    app.exec_()        